package com.item.yxlottery.yxlottery.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.utils.ToastUtils;
import com.item.yxlottery.yxlottery.R;
import com.item.yxlottery.yxlottery.adapter.OrderListAdapter;
import com.item.yxlottery.yxlottery.base.BaseActivity;
import com.item.yxlottery.yxlottery.bean.OrderBean;
import com.item.yxlottery.yxlottery.common.Constant;
import com.item.yxlottery.yxlottery.utils.UrlUtil;
import com.item.yxlottery.yxlottery.utils.Utils;
import com.orhanobut.hawk.Hawk;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by leo on 2017/2/25.
 * 我的订单
 */

public class MyOrdersActivity extends BaseActivity implements PullLoadMoreRecyclerView.PullLoadMoreListener {

    @Bind(R.id.myorders_tv_all)
    TextView mMyordersTvAll;
    @Bind(R.id.myorders_view_all)
    View mMyordersViewAll;
    @Bind(R.id.myorders_tv_waitpay)
    TextView mMyordersTvWaitpay;
    @Bind(R.id.myorders_view_waitpay)
    View mMyordersViewWaitpay;
    @Bind(R.id.myorders_tv_haspay)
    TextView mMyordersTvHaspay;
    @Bind(R.id.myorders_view_haspay)
    View mMyordersViewHaspay;
    @Bind(R.id.myorders_tv_hasissue)
    TextView mMyordersTvHasissue;
    @Bind(R.id.myorders_view_hasissue)
    View mMyordersViewHasissue;
    @Bind(R.id.myorders_tv_hasright)
    TextView mMyordersTvHasright;
    @Bind(R.id.myorders_view_hasright)
    View mMyordersViewHasright;
    @Bind(R.id.myorders_rlv_list)
    PullLoadMoreRecyclerView mMyordersRvList;
    @Bind(R.id.myorders_ll_tips)
    LinearLayout ll_tips;
    @Bind(R.id.myorders_ll_list)
    LinearLayout ll_list;

    private int page = 1;
    private String orderstate = "";
    private String count = "7";
    private ArrayList<OrderBean> mOrderList = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private OrderListAdapter mOrderListAdapter;

    @Override
    protected void setCustomActionBar(ImageButton ibActionbarLeft, TextView tvActionBarTitle, ImageButton ibActionbarRight, AutoRelativeLayout layoutActionbar) {
        ibActionbarLeft.setImageResource(R.drawable.leftarrow);
        tvActionBarTitle.setText("我的订单");
        ibActionbarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected View initView() {
        View view = View.inflate(this, R.layout.activity_myorders, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        setRecycleViewShow();
        getData();
    }


    @Override
    protected void initEvent() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.myorders_ll_all, R.id.myorders_ll_waitpay, R.id.myorders_ll_haspay, R.id.myorders_ll_hasissue, R.id.myorders_ll_hasright})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.myorders_ll_all://全部
                setView(1);
                setRefresh();
                orderstate = "";
                getData();
                break;
            case R.id.myorders_ll_waitpay://待付款
                setView(2);
                setRefresh();
                orderstate = "10";
                getData();
                break;
            case R.id.myorders_ll_haspay://待出票
                setView(3);
                setRefresh();
                orderstate = "20";
                getData();
                break;
            case R.id.myorders_ll_hasissue://待兑奖
                setView(4);
                setRefresh();
                orderstate = "30";
                getData();
                break;
            case R.id.myorders_ll_hasright://已开奖
                setView(5);
                setRefresh();
                orderstate = "40";
                getData();
                break;
        }
    }

    //获取数据
    private void getData() {

        OkHttpUtils.post().url(UrlUtil.getOrdersUrl()).addParams("appToken", (String) Hawk.get("appToken")).addParams("orderStates", orderstate)
                .addParams("page", "" + page).addParams("count", count).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                JSONObject parse = (JSONObject) JSON.parse(response);
                Utils.log("parse----------" + parse);

                if (parse.getString("error_code").equals(Constant.SUCCESS)) {
                    for (Object dataobject : JSON.parseArray(parse.getString("datas"))) {
                        JSONObject jsonObject = (JSONObject) dataobject;
                        OrderBean bean = new OrderBean();
                        bean.setOrderTotalPrice(jsonObject.getString("orderTotalPrice"));
                        bean.setOrderState(jsonObject.getString("orderState"));
                        bean.setCreateTimeString(jsonObject.getString("createTimeString"));

                        for (Object goodobject : JSON.parseArray(jsonObject.getString("orderGoodsList"))) {
                            JSONObject jsonObject1 = (JSONObject) goodobject;
                            bean.setLotteryPeriod(jsonObject1.getString("lotteryPeriod"));
                            bean.setGoodsName(jsonObject1.getString("goodsName"));
                            bean.setLotteryNumber(jsonObject1.getString("lotteryNumber"));
                            bean.setLotteryTimes(jsonObject1.getString("lotteryTimes"));
                        }
                        mOrderList.add(bean);
                        Utils.log("mOrderList----" + mOrderList.size());
                    }

                    if (mOrderList.size() == 0) {
                        ll_tips.setVisibility(View.VISIBLE);
                        ll_list.setVisibility(View.GONE);
                    } else {
                        ll_tips.setVisibility(View.GONE);
                        ll_list.setVisibility(View.VISIBLE);
                    }

                    mOrderListAdapter.notifyDataSetChanged();
                    mMyordersRvList.setPullLoadMoreCompleted();

                    if (page == 1) {//刷新滚到顶部，下拉加载时不变
                        mMyordersRvList.scrollToTop();
                    }

                } else {
                    ToastUtils.showShortToastSafe(MyOrdersActivity.this, parse.getString("msg"));
                }
            }
        });

    }

    //设置所选字体和line高亮
    private void setView(int i) {
        switch (i) {
            case 1:
                mMyordersTvAll.setTextColor(0xffff0000);
                mMyordersViewAll.setVisibility(View.VISIBLE);
                mMyordersTvWaitpay.setTextColor(0xff333333);
                mMyordersViewWaitpay.setVisibility(View.GONE);
                mMyordersTvHaspay.setTextColor(0xff333333);
                mMyordersViewHaspay.setVisibility(View.GONE);
                mMyordersTvHasissue.setTextColor(0xff333333);
                mMyordersViewHasissue.setVisibility(View.GONE);
                mMyordersTvHasright.setTextColor(0xff333333);
                mMyordersViewHasright.setVisibility(View.GONE);
                break;
            case 2:
                mMyordersTvAll.setTextColor(0xff333333);
                mMyordersViewAll.setVisibility(View.GONE);
                mMyordersTvWaitpay.setTextColor(0xffff0000);
                mMyordersViewWaitpay.setVisibility(View.VISIBLE);
                mMyordersTvHaspay.setTextColor(0xff333333);
                mMyordersViewHaspay.setVisibility(View.GONE);
                mMyordersTvHasissue.setTextColor(0xff333333);
                mMyordersViewHasissue.setVisibility(View.GONE);
                mMyordersTvHasright.setTextColor(0xff333333);
                mMyordersViewHasright.setVisibility(View.GONE);
                break;
            case 3:
                mMyordersTvAll.setTextColor(0xff333333);
                mMyordersViewAll.setVisibility(View.GONE);
                mMyordersTvWaitpay.setTextColor(0xff333333);
                mMyordersViewWaitpay.setVisibility(View.GONE);
                mMyordersTvHaspay.setTextColor(0xffff0000);
                mMyordersViewHaspay.setVisibility(View.VISIBLE);
                mMyordersTvHasissue.setTextColor(0xff333333);
                mMyordersViewHasissue.setVisibility(View.GONE);
                mMyordersTvHasright.setTextColor(0xff333333);
                mMyordersViewHasright.setVisibility(View.GONE);
                break;
            case 4:
                mMyordersTvAll.setTextColor(0xff333333);
                mMyordersViewAll.setVisibility(View.GONE);
                mMyordersTvWaitpay.setTextColor(0xff333333);
                mMyordersViewWaitpay.setVisibility(View.GONE);
                mMyordersTvHaspay.setTextColor(0xff333333);
                mMyordersViewHaspay.setVisibility(View.GONE);
                mMyordersTvHasissue.setTextColor(0xffff0000);
                mMyordersViewHasissue.setVisibility(View.VISIBLE);
                mMyordersTvHasright.setTextColor(0xff333333);
                mMyordersViewHasright.setVisibility(View.GONE);
                break;
            case 5:
                mMyordersTvAll.setTextColor(0xff333333);
                mMyordersViewAll.setVisibility(View.GONE);
                mMyordersTvWaitpay.setTextColor(0xff333333);
                mMyordersViewWaitpay.setVisibility(View.GONE);
                mMyordersTvHaspay.setTextColor(0xff333333);
                mMyordersViewHaspay.setVisibility(View.GONE);
                mMyordersTvHasissue.setTextColor(0xff333333);
                mMyordersViewHasissue.setVisibility(View.GONE);
                mMyordersTvHasright.setTextColor(0xffff0000);
                mMyordersViewHasright.setVisibility(View.VISIBLE);
                break;


        }

    }

    /*- 设置recycleview显示 -*/
    private void setRecycleViewShow() {//0:按距离排序,1:按销量排序
        //        OrderListAdapter orderListAdapter = new OrderListAdapter(MyOrdersActivity.this, R.layout.item_order_list, mOrderList);
        //        mMyordersRvList.setLayoutManager(new LinearLayoutManager(MyOrdersActivity.this));
        //        mMyordersRvList.setAdapter(orderListAdapter);
        //
        //获取mRecyclerView对象
        mRecyclerView = mMyordersRvList.getRecyclerView();
        //代码设置scrollbar无效？未解决！
        mRecyclerView.setVerticalScrollBarEnabled(true);
        //设置下拉刷新是否可见
        //mMyordersRvList.setRefreshing(true);
        //设置是否可以下拉刷新
        //mMyordersRvList.setPullRefreshEnable(true);
        //设置是否可以上拉刷新
        //mMyordersRvList.setPushRefreshEnable(false);
        //显示下拉刷新
        mMyordersRvList.setRefreshing(true);
        //设置上拉刷新文字
        mMyordersRvList.setFooterViewText("loading");
        //设置上拉刷新文字颜色
        //mMyordersRvList.setFooterViewTextColor(R.color.white);
        //设置加载更多背景色
        //mMyordersRvList.setFooterViewBackgroundColor(R.color.colorBackground);
        mMyordersRvList.setLinearLayout();

        mMyordersRvList.setOnPullLoadMoreListener(this);
        mOrderListAdapter = new OrderListAdapter(MyOrdersActivity.this, R.layout.item_order_list, mOrderList);
        mMyordersRvList.setAdapter(mOrderListAdapter);
    }

    @Override
    public void onRefresh() {
        Utils.log("onRefresh----");
        setRefresh();
        getData();
    }

    @Override
    public void onLoadMore() {
        Utils.log("onLoadMore----");
        page = page + 1;
        getData();
    }

    private void setRefresh() {
        mOrderList.clear();
        page = 1;
    }

}
